//
//  LNListViewController.swift
//  FearfulNotification
//
//  Created by 筱鹏 on 2018/1/18.
//  Copyright © 2018年 筱鹏. All rights reserved.
//

import UIKit
import UserNotifications

class LNListViewController: BaseViewController {

    let cellId = "LNListViewControllerTableViewCellId"
    
    var dataSource:[Any] = []
    
    var tableView: UITableView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = Theme.color.background
        self.configSubview()
        // Do any additional setup after loading the view.
        if #available(iOS 10.0, *) {
            LocalNotification.getPendingNotificationRequest { [weak self] (requests) in
                self?.dataSource = requests
                self?.tableView.reloadData()
            }
        } else {
            // Fallback on earlier versions
            LocalNotification.getLocalNotification(completionHandler: { [weak self] (notification) in
                self?.dataSource = notification
                self?.tableView.reloadData()
            })
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func configSubview() {
        
        tableView = UITableView(frame: self.view.bounds, style: .plain)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.backgroundColor = Theme.color.background
        tableView.rowHeight = 50
        self.view.addSubview(tableView)
    }

}

extension LNListViewController: UITableViewDelegate, UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataSource.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        var cell = tableView.dequeueReusableCell(withIdentifier: cellId)
        if cell == nil {
            cell = UITableViewCell(style: .subtitle, reuseIdentifier: cellId)
        }
        var title:String?
        var subtitle:String?
        if #available(iOS 10.0, *) {
            let requestOp = self.dataSource[indexPath.row] as? UNNotificationRequest
            if let request = requestOp {
                title = request.content.title
                subtitle = request.content.body
            }
        } else {
            let notifiOp = self.dataSource[indexPath.row] as? UILocalNotification
            if let notifi = notifiOp {
                title = notifi.alertTitle
                subtitle = notifi.alertBody
            }
        }
        cell?.textLabel?.text = title
        cell?.textLabel?.textColor = Theme.color.title
        cell?.detailTextLabel?.text = subtitle
        cell?.detailTextLabel?.textColor = Theme.color.description
        cell?.detailTextLabel?.numberOfLines = 0
        cell?.backgroundColor = Theme.color.background
        return cell!
    }
    
    func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
        let deleteAction = UITableViewRowAction(style: .destructive, title: "删除") { [weak self] (action, idxPath) in
            self?.removeDataSourceData(idxPath)
            self?.tableView.deleteRows(at: [idxPath], with: .automatic)
        }
        return [deleteAction]
    }
    
    func removeDataSourceData(_ idxPath: IndexPath) {
        if #available(iOS 10.0, *) {
            let requestOp = self.dataSource[idxPath.row] as? UNNotificationRequest
            guard let request = requestOp else { return }
            LocalNotification.remove(identifier: request.identifier)
        } else {
            let notifiOp = self.dataSource[idxPath.row] as? UILocalNotification
            guard let notifi = notifiOp else { return }
            guard let userInfo = notifi.userInfo else {return}
            guard let iden = userInfo[FNLNIdentifierKey] as? String else {return}
            LocalNotification.remove(identifier: iden)
        }
        self.dataSource.remove(at: idxPath.row)
    }
}
